package com.ruoyi.erp.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.erp.domain.XzgCustomer;
import com.ruoyi.erp.mapper.XzgCustomerMapper;
import com.ruoyi.erp.mapper.XzgOrderMapper;
import com.ruoyi.erp.service.IXzgCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author qiuwencong
 * @Date 2021/12/2 9:23
 * @Package com.ruoyi.erp.service.impl
 * @Description
 */
@Service
public class XzgCustomerServiceImpl implements IXzgCustomerService {

    @Autowired
    private XzgCustomerMapper xzgCustomerMapper;

    @Autowired
    private XzgOrderMapper xzgOrderMapper;

    @Override
    public XzgCustomer selectCustomerById(Long customerId) {
        return xzgCustomerMapper.selectByPrimaryKey(customerId);
    }

    @Override
    public int insertCustomer(XzgCustomer customer) {
        return xzgCustomerMapper.insertSelective(customer);
    }

    @Override
    public int updateCustomer(XzgCustomer customer) {
        return xzgCustomerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public List<XzgCustomer> selectCustomerList(XzgCustomer customer) {
        return xzgCustomerMapper.selectCustomerList(customer);
    }

    @Override
    public int deleteCustomerById(Long[] customerIds, String username) {
        if (xzgOrderMapper.selectByCustomerIds(customerIds) > 0) {
            throw new ServiceException("存在未完成的订单，请取消或完成订单后删除");
        }
        return xzgCustomerMapper.logicDeleteCustomerById(customerIds, username);
    }

    @Override
    public List<XzgCustomer> selectCustomerNames() {
        List<XzgCustomer> customers = xzgCustomerMapper.selectCustomerList(new XzgCustomer());
        customers.parallelStream().forEach(customer -> {
            customer.setCustomerName(customer.getCustomerName() + "/" + customer.getCompany() + "/" + customer.getDepartment());
        });
        return customers;
    }
}
